const stream = require("stream")
const fs = require("fs")
// const { buffer } = require("stream/consumers")

const rs = fs.createReadStream("./images/1.txt", "utf-8")

rs.on("data", chunk => {
    console.log(`DATA:>>>${chunk}<<<\n`)
})

rs.on("end", () => {
    console.log("文件流读取操作...")
})

rs.on("error", err => {
    console.log(`ERROR: ${err}`)
})

const ws1 = fs.createWriteStream("./images/2.txt", "utf8")
ws1.write("使用Stream写入文本数据。。。\n")
ws1.write(">>END。");
ws1.end


const ws2 = fs.createWriteStream("./images/3.txt", "utf8")
ws2.write(Buffer.from("使用Stream写入二进制数据...\n", "utf-8"))
ws2.write(Buffer.from(">>>END.", "utf-8"))
ws2.end()

//文件流管道：拷贝
const rsSrc = fs.createReadStream("./images/3.txt")
const wsDst = fs.createWriteStream("./images/4.txt")
rsSrc.pipe(wsDst)